"""
Case Type   : 拷贝数据
Case Name   : stdout进行copy to
Create At   : 2024/7/25
Owner       : songjing20
Description :
    1.创建测试表并插入数据
    2.执行copy to stdout
    3.删除表
Expect      :
    1.成功
    2.导出到标准输出成功
    3.成功
History     : 
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class CopyFile(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.commonsh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.tb_name = 't_copy_0053'

    def test_copy_file(self):
        text = '-----step1:创建测试表并插入数据;expect:成功-----'
        self.log.info(text)
        preset_res = self.commonsh.execut_db_sql(
            f"drop table if exists {self.tb_name};"
            f"create table {self.tb_name} (sk integer,id varchar(16),"
            f"name varchar(20),sq_ft integer);"
            f"insert into {self.tb_name} values (001,'sk1','tt1',3331);"
            f"insert into {self.tb_name} values (002,'sk2','tt2',3332);"
            f"insert into {self.tb_name} values (003,'sk3','tt3',3333);")
        self.log.info(preset_res)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, preset_res,
                      f'执行失败:{text}')
        self.assertEqual(preset_res.count(self.Constant.INSERT_SUCCESS_MSG),
                         3, f'执行失败:{text}')

        text = '-----step2:执行copy to stdout;expect:导出到标准输出成功-----'
        self.log.info(text)
        copy_res = self.commonsh.execut_db_sql(
            f"copy {self.tb_name} to stdout;")
        self.log.info(copy_res)
        self.assertIn('1\tsk1\ttt1\t3331\n2\tsk2\ttt2\t3332\n3\tsk3\ttt3\t3333',
                      copy_res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step3:删除表;expect:成功-----'
        self.log.info(text)
        res = self.commonsh.execut_db_sql(f"drop table {self.tb_name} cascade")
        self.log.info(res)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, res, f'执行失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
